package com.shiguo365.market.presenter.implement;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.shiguo365.market.display.activity.LoginActivity;
import com.shiguo365.market.display.activity.MainActivity;
import com.shiguo365.market.display.view.ICartView;
import com.shiguo365.market.display.view.ILoginView;
import com.shiguo365.market.presenter.contract.ILoginPresenter;

import biz.yfsoft.app.aengine.api.AEClient;
import biz.yfsoft.app.aengine.api.AEParam;
import biz.yfsoft.app.aengine.api.BaseAECallback;

import static android.support.v4.app.ActivityCompat.startActivity;

/**
 * Created by caomenglong on 16/7/27.
 */
public class LoginPresenter implements ILoginPresenter {
    String TAG="LoginPresenter";
    private ILoginView iLoginView;
    private Activity activity;

    public LoginPresenter(@NonNull Activity activity,@NonNull ILoginView loginView) {
        this.iLoginView = loginView;
        this.activity=activity;
    }


    @Override
    public void login(String username, String password) {
        AEClient _client = new AEClient("shiguo.user.signIn");
        AEParam aeParam =new AEParam();
        aeParam.set("username",username);
        aeParam.set("password",password);
        aeParam.set("source",2);
        _client.call(aeParam,new BaseAECallback() {

            @Override
            public void onSuccess(JSONObject a) {
                Log.d(TAG,a.toJSONString());
                int uid=a.getInteger("id");
                SharedPreferences.Editor editor =activity.getSharedPreferences("data", Context.MODE_PRIVATE).edit();
                editor.putInt("uid",uid);
                editor.commit();
                iLoginView.LoginSuccess();

            }

            @Override
            public void onError( String errno, String message) {
                Log.e(TAG,"onFailure ......." + errno + ";message:" + message);
                iLoginView.ShowMessages("用户名和密码错误!");
            }

            @Override
            public void onFinally() {
                //mView.onGetUserInfoFinished();
                Log.d(TAG,"zzzzz");
            }
        });
    }
}
